package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public OverviewVo getOverview() {
        //1. 查询redis缓存中的数据
        log.info("从Redis中,获取线索和商机的概览数据");
        Object dataOverview = redisTemplate.opsForValue().get("DATA_OVERVIEW");
        if (dataOverview != null) {
            log.info("查询Redis缓存中的数据:{}", dataOverview);
            return (OverviewVo) dataOverview;
        }
        // 2. 查询数据库
        // 2.1. 获取线索对应数据
        OverviewVo clueOverview = clueMapper.getClueOverview();
        // 2.2. 获取商机对应数据
        OverviewVo businessOverview = businessMapper.getBusinessOverview();
        // 2.3.封装 Overview
        BeanUtils.copyProperties(clueOverview, businessOverview, "businessTotal", "businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");
        // 3. 缓存数据
        log.info("查询数据库中的数据:{},缓存到Redis中", businessOverview);
        redisTemplate.opsForValue().set("DATA_OVERVIEW", businessOverview, 1, TimeUnit.MINUTES);
        return businessOverview;
    }
}
